From 3d7c8915f60e4f85de01acf582ed3a4fad0061e8 Mon Sep 17 00:00:00 2001 From: robertl Date: Tue, 16 Feb 2010 00:28:35 +0000 Subject: [PATCH] Martin Buck adds support for packet IDs and sizes > 255. --- gpsbabel/jeeps/gps.h | 12 +++-- gpsbabel/jeeps/gpsapp.c | 28 +++++------ gpsbabel/jeeps/gpsapp.h | 2 +- gpsbabel/jeeps/gpscom.c | 2 +- gpsbabel/jeeps/gpsdevice.c | 6 ++- gpsbabel/jeeps/gpsdevice.h | 2 - gpsbabel/jeeps/gpsdevice_ser.c | 1 - gpsbabel/jeeps/gpsdevice_usb.c | 1 - gpsbabel/jeeps/gpsmem.c | 7 +-- gpsbabel/jeeps/gpsprot.h | 4 +- gpsbabel/jeeps/gpsread.c | 9 ++-- gpsbabel/jeeps/gpssend.c | 88 ++++++++++++++++++---------------- gpsbabel/jeeps/gpssend.h | 3 +- gpsbabel/jeeps/gpsusbcommon.c | 9 ++-- gpsbabel/jeeps/gpsusbread.c | 2 +- gpsbabel/jeeps/gpsusbsend.c | 17 ------- 16 files changed, 90 insertions(+), 103 deletions(-) diff --git a/gpsbabel/jeeps/gps.h b/gpsbabel/jeeps/gps.h index 42018a6a2..54c492f96 100644 --- a/gpsbabel/jeeps/gps.h +++ b/gpsbabel/jeeps/gps.h @@ -37,6 +37,13 @@ extern char gps_categories[16][17]; typedef struct GPS_SPacket +{ + US type; + uint32 n; + UC *data; +} GPS_OPacket, *GPS_PPacket; + +typedef struct GPS_Serial_SPacket { UC dle; UC type; @@ -45,10 +52,7 @@ typedef struct GPS_SPacket UC chk; UC edle; UC etx; - UC bytes; /* Actual number of bytes (for sending) */ -} GPS_OPacket, *GPS_PPacket; - - +} GPS_Serial_OPacket, *GPS_Serial_PPacket; typedef struct GPS_SProduct_Data_Type { diff --git a/gpsbabel/jeeps/gpsapp.c b/gpsbabel/jeeps/gpsapp.c index 81ef74c90..9bcb95a6e 100644 --- a/gpsbabel/jeeps/gpsapp.c +++ b/gpsbabel/jeeps/gpsapp.c @@ -985,7 +985,7 @@ int32 GPS_A100_Send(const char *port, GPS_PWay *way, int32 n, int (*cb)(GPS_PWay } GPS_Make_Packet(&tra, LINK_ID[gps_link_type].Pid_Wpt_Data, - data, (US) len); + data, len); if(!GPS_Write_Packet(fd,tra)) return gps_errno; @@ -2922,7 +2922,7 @@ int32 GPS_A200_Send(const char *port, GPS_PWay *way, int32 n) GPS_PPacket rec; int32 i; int32 len; - UC method; + US method; if(!GPS_Device_On(port,&fd)) return gps_errno; @@ -3019,7 +3019,7 @@ int32 GPS_A200_Send(const char *port, GPS_PWay *way, int32 n) } - GPS_Make_Packet(&tra, method, data,(US) len); + GPS_Make_Packet(&tra, method, data, len); if(!GPS_Write_Packet(fd,tra)) return gps_errno; @@ -3071,7 +3071,7 @@ int32 GPS_A201_Send(const char *port, GPS_PWay *way, int32 n) GPS_PPacket rec; int32 i; int32 len; - UC method; + US method; if(!GPS_Device_On(port,&fd)) return gps_errno; @@ -3188,7 +3188,7 @@ int32 GPS_A201_Send(const char *port, GPS_PWay *way, int32 n) } - GPS_Make_Packet(&tra, method, data,(US) len); + GPS_Make_Packet(&tra, method, data, len); if(!GPS_Write_Packet(fd,tra)) return gps_errno; @@ -3776,7 +3776,7 @@ int32 GPS_A300_Send(const char *port, GPS_PTrack *trk, int32 n) } GPS_Make_Packet(&tra, LINK_ID[gps_link_type].Pid_Trk_Data, - data,(US) len); + data, len); if(!GPS_Write_Packet(fd,tra)) return gps_errno; @@ -3828,7 +3828,7 @@ int32 GPS_A301_Send(const char *port, GPS_PTrack *trk, int32 n) GPS_PPacket rec; int32 i; int32 len; - UC method; + US method; if(gps_trk_transfer == -1) return GPS_UNSUPPORTED; @@ -3912,14 +3912,14 @@ int32 GPS_A301_Send(const char *port, GPS_PTrack *trk, int32 n) } - GPS_Make_Packet(&tra, method, data,(US) len); + GPS_Make_Packet(&tra, method, data, len); if(!GPS_Write_Packet(fd,tra)) return gps_errno; if(!GPS_Get_Ack(fd, &tra, &rec)) { - GPS_Error("A301_Send: Track packet not acknowledgedn"); + GPS_Error("A301_Send: Track packet not acknowledged"); return FRAMING_ERROR; } } @@ -4714,7 +4714,7 @@ int32 GPS_A400_Send(const char *port, GPS_PWay *way, int32 n) } GPS_Make_Packet(&tra, LINK_ID[gps_link_type].Pid_Prx_Wpt_Data, - data,(US) len); + data, len); if(!GPS_Write_Packet(fd,tra)) return gps_errno; @@ -5174,7 +5174,7 @@ int32 GPS_A500_Send(const char *port, GPS_PAlmanac *alm, int32 n) } GPS_Make_Packet(&tra, LINK_ID[gps_link_type].Pid_Almanac_Data, - data,(US) len); + data, len); if(!GPS_Write_Packet(fd,tra)) return gps_errno; @@ -6070,7 +6070,7 @@ int32 GPS_A906_Get(const char *port, GPS_PLap **lap, pcb_fn cb) return MEMORY_ERROR; GPS_Util_Put_Short(data, - COMMAND_ID[gps_device_command].Cmnd_Transfer_Lap); + COMMAND_ID[gps_device_command].Cmnd_Transfer_Laps); GPS_Make_Packet(&trapkt, LINK_ID[gps_link_type].Pid_Command_Data, data,2); if(!GPS_Write_Packet(fd,trapkt)) @@ -6246,7 +6246,7 @@ void GPS_D1011b_Get(GPS_PLap *Lap, UC *p) * but they really are runtime variable. Sigh. */ const char * -Get_Pkt_Type(unsigned char p, unsigned short d0, const char **xinfo) +Get_Pkt_Type(US p, US d0, const char **xinfo) { *xinfo = NULL; #define LT LINK_ID[gps_link_type] @@ -6325,7 +6325,7 @@ Get_Pkt_Type(unsigned char p, unsigned short d0, const char **xinfo) return "FLIBOO"; if (p == LT.Pid_Lap) return "LAPDAT"; - if (p == LT.Pid_Wpt_Cat_Data) + if (p == LT.Pid_Wpt_Cat) return "WPTCAT"; if (p == LT.Pid_Run) return "RUNDAT"; diff --git a/gpsbabel/jeeps/gpsapp.h b/gpsbabel/jeeps/gpsapp.h index b303322d9..04ed23528 100644 --- a/gpsbabel/jeeps/gpsapp.h +++ b/gpsbabel/jeeps/gpsapp.h @@ -96,7 +96,7 @@ Capability A918: D918 Capability A1013: D1014 */ -const char * Get_Pkt_Type(unsigned char p, unsigned short d0, const char **xinfo); +const char * Get_Pkt_Type(US p, US d0, const char **xinfo); #endif diff --git a/gpsbabel/jeeps/gpscom.c b/gpsbabel/jeeps/gpscom.c index 3c5276ec2..883593ff3 100644 --- a/gpsbabel/jeeps/gpscom.c +++ b/gpsbabel/jeeps/gpscom.c @@ -648,7 +648,7 @@ int32 GPS_Command_Pvt_Get(gpsdevh **fd, GPS_PPvt_Data *pvt) ** Get lap from GPS ** ** @param [r] port [const char *] serial port -** @param [w] way [GPS_PLap **] pointer to lap array +** @param [w] lap [GPS_PLap **] pointer to lap array ** ** @return [int32] number of lap entries ************************************************************************/ diff --git a/gpsbabel/jeeps/gpsdevice.c b/gpsbabel/jeeps/gpsdevice.c index 90c841f1b..8c68bd6a0 100644 --- a/gpsbabel/jeeps/gpsdevice.c +++ b/gpsbabel/jeeps/gpsdevice.c @@ -80,7 +80,9 @@ int32 GPS_Get_Ack(gpsdevh * fd, GPS_PPacket *tra, GPS_PPacket *rec) return (ops->Get_Ack)(fd, tra, rec); } -void GPS_Make_Packet(GPS_PPacket *packet, UC type, UC *data, int16 n) +void GPS_Make_Packet(GPS_PPacket *packet, US type, UC *data, uint32 n) { - (ops->Make_Packet)(packet, type, data, n); + (*packet)->type = type; + memcpy((*packet)->data, data, n); + (*packet)->n = n; } diff --git a/gpsbabel/jeeps/gpsdevice.h b/gpsbabel/jeeps/gpsdevice.h index eb5d53d96..c428ea158 100644 --- a/gpsbabel/jeeps/gpsdevice.h +++ b/gpsbabel/jeeps/gpsdevice.h @@ -52,7 +52,6 @@ typedef int32 (*gps_device_op5)(const char *, gpsdevh **fd); typedef int32 (*gps_device_op10)(gpsdevh * fd, GPS_PPacket *tra, GPS_PPacket *rec); typedef int32 (*gps_device_op12)(gpsdevh * fd, GPS_PPacket packet); typedef int32 (*gps_device_op13)(gpsdevh * fd, GPS_PPacket *packet); -typedef void (*gps_device_op14)(GPS_PPacket *packet, UC type, UC *data, int16 n); typedef struct { gps_device_op5 Device_On; gps_device_op Device_Off; @@ -62,7 +61,6 @@ typedef struct { gps_device_op10 Send_Ack; gps_device_op10 Get_Ack; gps_device_op13 Read_Packet; - gps_device_op14 Make_Packet; gps_device_op12 Write_Packet; } gps_device_ops; diff --git a/gpsbabel/jeeps/gpsdevice_ser.c b/gpsbabel/jeeps/gpsdevice_ser.c index 29ff476e6..ae6c3cd17 100644 --- a/gpsbabel/jeeps/gpsdevice_ser.c +++ b/gpsbabel/jeeps/gpsdevice_ser.c @@ -32,6 +32,5 @@ gps_device_ops gps_serial_ops = { GPS_Serial_Send_Ack, GPS_Serial_Get_Ack, GPS_Serial_Packet_Read, - GPS_Serial_Make_Packet, GPS_Serial_Write_Packet, }; diff --git a/gpsbabel/jeeps/gpsdevice_usb.c b/gpsbabel/jeeps/gpsdevice_usb.c index dd578c829..32cb0ef73 100644 --- a/gpsbabel/jeeps/gpsdevice_usb.c +++ b/gpsbabel/jeeps/gpsdevice_usb.c @@ -54,6 +54,5 @@ gps_device_ops gps_usb_ops = { (gps_device_op10) success_stub, (gps_device_op10) success_stub, gdu_read, - GPS_Make_Packet_usb, GPS_Write_Packet_usb }; diff --git a/gpsbabel/jeeps/gpsmem.c b/gpsbabel/jeeps/gpsmem.c index e9ae4a3ec..bfc92bcb7 100644 --- a/gpsbabel/jeeps/gpsmem.c +++ b/gpsbabel/jeeps/gpsmem.c @@ -56,9 +56,6 @@ GPS_PPacket GPS_Packet_New(void) return NULL; } - ret->dle = ret->edle = DLE; - ret->etx = ETX; - return ret; } @@ -301,7 +298,7 @@ void GPS_Way_Del(GPS_PWay *thys) ** ** Lap constructor ** -** @return [GPS_PLap] virgin track +** @return [GPS_PLap] virgin lap **********************************************************************/ GPS_PLap GPS_Lap_New(void) @@ -325,7 +322,7 @@ GPS_PLap GPS_Lap_New(void) ** ** Lap destructor ** -** @param [w] thys [GPS_PLap *] track to delete +** @param [w] thys [GPS_PLap *] lap to delete ** ** @return [void] **********************************************************************/ diff --git a/gpsbabel/jeeps/gpsprot.h b/gpsbabel/jeeps/gpsprot.h index 2e30d20c9..09df347eb 100644 --- a/gpsbabel/jeeps/gpsprot.h +++ b/gpsbabel/jeeps/gpsprot.h @@ -38,7 +38,7 @@ struct LINKDATA US Pid_FlightBook_Record; US Pid_Lap; - US Pid_Wpt_Cat_Data; + US Pid_Wpt_Cat; US Pid_Run; US Pid_Workout; US Pid_Workout_Occurrence; @@ -81,7 +81,7 @@ struct COMMANDDATA US Cmnd_Start_Pvt_Data; US Cmnd_Stop_Pvt_Data; US Cmnd_FlightBook_Transfer; - US Cmnd_Transfer_Lap; + US Cmnd_Transfer_Laps; US Cmnd_Transfer_Wpt_Cats; US Cmnd_Transfer_Runs; US Cmnd_Transfer_Workouts; diff --git a/gpsbabel/jeeps/gpsread.c b/gpsbabel/jeeps/gpsread.c index 587f15c6f..2bf926b1d 100644 --- a/gpsbabel/jeeps/gpsread.c +++ b/gpsbabel/jeeps/gpsread.c @@ -77,7 +77,7 @@ int32 GPS_Serial_Packet_Read(gpsdevh *fd, GPS_PPacket *packet) int32 isDLE; UC *p; int32 i; - UC chk=0; + UC chk=0, chk_read; const char *m1; const char *m2; @@ -103,7 +103,6 @@ int32 GPS_Serial_Packet_Read(gpsdevh *fd, GPS_PPacket *packet) if(!len) { - (*packet)->dle = u; if(u != DLE) { (void) fprintf(stderr,"GPS_Packet_Read: No DLE. Data received, but probably not a garmin packet.\n"); @@ -141,21 +140,19 @@ int32 GPS_Serial_Packet_Read(gpsdevh *fd, GPS_PPacket *packet) if(u == ETX) if(isDLE) { - (*packet)->edle = DLE; - (*packet)->etx = ETX; if(p-(*packet)->data-2 != (*packet)->n) { GPS_Error("GPS_Packet_Read: Bad count"); gps_errno = FRAMING_ERROR; return 0; } - (*packet)->chk = *(p-2); + chk_read = *(p-2); for(i=0,p=(*packet)->data;i<(*packet)->n;++i) chk -= *p++; chk -= (*packet)->type; chk -= (*packet)->n; - if(chk != (*packet)->chk) + if(chk != chk_read) { GPS_Error("CHECKSUM: Read error\n"); gps_errno = FRAMING_ERROR; diff --git a/gpsbabel/jeeps/gpssend.c b/gpsbabel/jeeps/gpssend.c index d863683d9..80801a0c7 100644 --- a/gpsbabel/jeeps/gpssend.c +++ b/gpsbabel/jeeps/gpssend.c @@ -30,69 +30,65 @@ #include -/* @func GPS_Make_Packet *********************************************** +/* @funcstatic Build_Serial_Packet ************************************* ** -** Forms a complete packet to send -** -** @param [w] packet [GPS_PPacket *] packet string -** @param [r] type [UC] packet type -** @param [r] data [UC *] data string -** @param [r] n [int16] number of bytes in data string +** Forms a complete packet to send on serial port +* +** @param [r] in [GPS_PPacket *] packet string with portable packet data +** @param [w] out [GPS_Serial_PPacket *] packet string suitable for serial port ** -** @return [void] +** @return [US] number of data bytes to send ************************************************************************/ - -void GPS_Serial_Make_Packet(GPS_PPacket *packet, UC type, UC *data, int16 n) +static US +Build_Serial_Packet(GPS_PPacket in, GPS_Serial_PPacket out) { UC *p; UC *q; int32 i; UC chk=0; + US bytes=0; - - p = data; - q = (*packet)->data; + p = in->data; + q = out->data; - (*packet)->dle = DLE; - (*packet)->edle = DLE; - (*packet)->etx = ETX; - (*packet)->n = (UC) n; - (*packet)->type = type; - (*packet)->bytes = 0; + out->dle = DLE; + out->edle = DLE; + out->etx = ETX; + out->n = in->n; + out->type = in->type; - chk -= type; + chk -= in->type; - if(n == DLE) + if(in->n == DLE) { - ++(*packet)->bytes; + ++bytes; *q++ = DLE; } + chk -= in->n; - chk -= (UC) n; - - for(i=0;in; ++i) { if(*p == DLE) { - ++(*packet)->bytes; + ++bytes; *q++ = DLE; } chk -= *p; *q++ = *p++; - ++(*packet)->bytes; + ++bytes; } if(chk == DLE) { *q++ = DLE; - ++(*packet)->bytes; + ++bytes; } - (*packet)->chk = chk; + out->chk = chk; - return; + return bytes; } @@ -130,11 +126,21 @@ int32 GPS_Serial_Write_Packet(gpsdevh *fd, GPS_PPacket packet) { size_t ret; const char *m1, *m2; - + GPS_Serial_OPacket ser_pkt; + UC ser_pkt_data[MAX_GPS_PACKET_SIZE * sizeof(UC)]; + US bytes; + if (packet->type >= 0xff || packet->n >= 0xff) { + GPS_Error("SEND: Unsupported packet type/size for serial protocol"); + return 0; + } + + ser_pkt.data = ser_pkt_data; + bytes = Build_Serial_Packet(packet, &ser_pkt); + GPS_Diag("Tx Data:"); - Diag(&packet->dle, 3); - if((ret=GPS_Serial_Write(fd,(const void *) &packet->dle,(size_t)3)) == -1) + Diag(&ser_pkt.dle, 3); + if((ret=GPS_Serial_Write(fd,(const void *) &ser_pkt.dle,(size_t)3)) == -1) { perror("write"); GPS_Error("SEND: Write to GPS failed"); @@ -146,29 +152,29 @@ int32 GPS_Serial_Write_Packet(gpsdevh *fd, GPS_PPacket packet) return 0; } - Diag(packet->data, packet->bytes); - if((ret=GPS_Serial_Write(fd,(const void *)packet->data,(size_t)packet->bytes)) == -1) + Diag(ser_pkt.data, bytes); + if((ret=GPS_Serial_Write(fd,(const void *)ser_pkt.data,(size_t)bytes)) == -1) { perror("write"); GPS_Error("SEND: Write to GPS failed"); return 0; } - if(ret!=packet->bytes) + if(ret!=bytes) { GPS_Error("SEND: Incomplete write to GPS"); return 0; } - Diag(&packet->chk, 3); + Diag(&ser_pkt.chk, 3); GPS_Diag(": "); - DiagS(packet->data, packet->bytes); - DiagS(&packet->chk, 3); - m1 = Get_Pkt_Type(packet->type, packet->data[0], &m2); + DiagS(ser_pkt.data, bytes); + DiagS(&ser_pkt.chk, 3); + m1 = Get_Pkt_Type(ser_pkt.type, ser_pkt.data[0], &m2); GPS_Diag("(%-8s%s)\n", m1, m2 ? m2 : ""); - if((ret=GPS_Serial_Write(fd,(const void *)&packet->chk,(size_t)3)) == -1) + if((ret=GPS_Serial_Write(fd,(const void *)&ser_pkt.chk,(size_t)3)) == -1) { perror("write"); GPS_Error("SEND: Write to GPS failed"); diff --git a/gpsbabel/jeeps/gpssend.h b/gpsbabel/jeeps/gpssend.h index edd3f5b0d..2b6fc9115 100644 --- a/gpsbabel/jeeps/gpssend.h +++ b/gpsbabel/jeeps/gpssend.h @@ -11,11 +11,10 @@ extern "C" #define GPS_ARB_LEN 1024 -void GPS_Serial_Make_Packet(GPS_PPacket *packet, UC type, UC *data, int16 n); int32 GPS_Serial_Write_Packet(gpsdevh *fd, GPS_PPacket packet); int32 GPS_Serial_Send_Ack(gpsdevh *fd, GPS_PPacket *tra, GPS_PPacket *rec); -void GPS_Make_Packet(GPS_PPacket *packet, UC type, UC *data, int16 n); +void GPS_Make_Packet(GPS_PPacket *packet, US type, UC *data, uint32 n); #endif diff --git a/gpsbabel/jeeps/gpsusbcommon.c b/gpsbabel/jeeps/gpsusbcommon.c index 35ac8d684..26d1264c3 100644 --- a/gpsbabel/jeeps/gpsusbcommon.c +++ b/gpsbabel/jeeps/gpsusbcommon.c @@ -84,6 +84,7 @@ gusb_cmd_get(garmin_usb_packet *ibuf, size_t sz) int rv; unsigned char *buf = (unsigned char *) &ibuf->dbuf; int orig_receive_state; + unsigned short pkt_id; top: orig_receive_state = receive_state; switch (receive_state) { @@ -97,6 +98,7 @@ top: fatal("Unknown receiver state %d\n", receive_state); } + pkt_id = le_read16(&ibuf->gusb_pkt.pkt_id); if (gps_show_bytes) { int i; const char *m1, *m2; @@ -121,13 +123,13 @@ top: GPS_Diag("%c", isalnum(buf[i])? buf[i] : '.'); } - m1 = Get_Pkt_Type(ibuf->gusb_pkt.pkt_id[0], pkttype, &m2); + m1 = Get_Pkt_Type(pkt_id, pkttype, &m2); if ((rv == 0) && (receive_state == rs_frombulk) ) {m1= "RET2INTR";m2=NULL;}; GPS_Diag("(%-8s%s)\n", m1, m2 ? m2 : ""); } /* Adjust internal state and retry the read */ - if ((rv > 0) && (ibuf->gusb_pkt.pkt_id[0] == GUSB_REQUEST_BULK)) { + if ((rv > 0) && (pkt_id == GUSB_REQUEST_BULK)) { receive_state = rs_frombulk; goto top; } @@ -158,6 +160,7 @@ gusb_cmd_send(const garmin_usb_packet *opkt, size_t sz) if (gps_show_bytes) { const unsigned short pkttype = le_read16(&opkt->gusb_pkt.databuf[0]); + const unsigned short pkt_id = le_read16(&opkt->gusb_pkt.pkt_id); GPS_Diag("TX [%d]:", sz); for(i=0;igusb_pkt.pkt_id[0], pkttype, &m2); + m1 = Get_Pkt_Type(pkt_id, pkttype, &m2); GPS_Diag("(%-8s%s)\n", m1, m2 ? m2 : ""); } diff --git a/gpsbabel/jeeps/gpsusbread.c b/gpsbabel/jeeps/gpsusbread.c index e266011dd..d061f312a 100644 --- a/gpsbabel/jeeps/gpsusbread.c +++ b/gpsbabel/jeeps/gpsusbread.c @@ -71,7 +71,7 @@ do_over: */ (*packet)->type = le_read16(&pkt.gusb_pkt.pkt_id); payload_size = le_read32(&pkt.gusb_pkt.datasz); - (*packet)->n = (UC) payload_size; + (*packet)->n = payload_size; memcpy((*packet)->data, &pkt.gusb_pkt.databuf, payload_size); return 1; diff --git a/gpsbabel/jeeps/gpsusbsend.c b/gpsbabel/jeeps/gpsusbsend.c index bd376b7e0..9aee54c7e 100644 --- a/gpsbabel/jeeps/gpsusbsend.c +++ b/gpsbabel/jeeps/gpsusbsend.c @@ -25,23 +25,6 @@ #include "garminusb.h" #include "gpsusbint.h" -void -GPS_Make_Packet_usb(GPS_PPacket *packet, UC type, UC *data, int16 n) -{ - /* - * For the USB case, it's a little tacky that we just copy - * the params into *packet, but we really don't have any manipulations - * to do here. They're done in send_packet in order to keep the - * contents of *packet identical for the serial and USB cases. - */ - - (*packet)->type = type; - memcpy((*packet)->data, data, n); - (*packet)->n = (UC) n; - - return; -} - int32 GPS_Write_Packet_usb(gpsdevh *dh, GPS_PPacket packet) { -- 2.30.2